package news;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.app.badresden.R;

public class NewsArrayAdapter extends ArrayAdapter<News> {

	private List<News> newsList;
	private Activity context;

	private static int maxLengthMsg = 90;

	public NewsArrayAdapter(Activity context, List<News> newsList) {

		super(context, R.layout.news_listlayout, newsList);
		this.context = context;
		this.newsList = newsList;
	}

	static class ViewHolder {

		protected TextView textViewDate;
		protected TextView textViewMessage;
		protected TextView textViewAuthor;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = null;

		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.news_listlayout, null);

			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.textViewDate = (TextView) view.findViewById(R.id.news_datum);
			viewHolder.textViewMessage = (TextView) view.findViewById(R.id.news_text);
			viewHolder.textViewAuthor = (TextView) view.findViewById(R.id.news_autor);
			view.setTag(viewHolder);
		}
		else
			view = convertView;

		ViewHolder holder = (ViewHolder) view.getTag();

		String msg = newsList.get(position).getMessage();

		if (msg.length() > maxLengthMsg) msg = msg.substring(0, maxLengthMsg) + "...";

		holder.textViewDate.setText(newsList.get(position).getDate());
		holder.textViewMessage.setText(msg);
		holder.textViewAuthor.setText(newsList.get(position).getAuthor());

		return view;
	}
}
